package cn.ekuma.epos.crm.contract;

import java.awt.BorderLayout;
import java.util.Date;
import java.util.Properties;

import com.openbravo.bean.crm.contract.ContractProduct;
import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.panels.AbstractRefProductEditor;
import cn.ekuma.epos.datalogic.I_DataLogicSales;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JContractProductEditor extends AbstractRefProductEditor<ContractProduct>{
	private JTextField txtContractPrice;
	private JTextField txtMaxPrice;
	private JTextField txtMinPrice;
	private JTextField txtVDate;
	private JTextField txtPriceBuy;
	private JTextField txtPriceSell;
	private JTextField txtPriceCustomer;
	private ComboBoxValModel stateModel;
	private ComboBoxValModel distributionModel;
	private JComboBox comboBox_1;
	private JComboBox comboBox;
	private JButton btnNewButton;

	public JContractProductEditor(I_DataLogicSales app,DirtyManager dirty) {
		super(app,dirty,false);
		stateModel=new ComboBoxValModel();
		stateModel.add(new ComboBoxValEntry(
				ContractProduct.STATE_Sale,"正常" ));
		stateModel.add(new ComboBoxValEntry(
				ContractProduct.STATE_noSale,"停购" ));
		comboBox_1.setModel(stateModel);
		
		distributionModel=new ComboBoxValModel();
		distributionModel.add(new ComboBoxValEntry(
				ContractProduct.DISTRIBUTIONMODE_purchase,"购销" ));
		distributionModel.add(new ComboBoxValEntry(
				ContractProduct.DISTRIBUTIONMODE_dealer,"代销" ));
		distributionModel.add(new ComboBoxValEntry(
				ContractProduct.DISTRIBUTIONMODE_lease,"租赁" ));
		comboBox.setModel(distributionModel);
		
	}
	protected void initComponents() {
		super.initComponents();
		
		JPanel panel1 = new JPanel();
		panel1.setPreferredSize(new Dimension(10, 300));
	    panel1.setLayout(null);
		
		JLabel label = new JLabel("约定价");
		label.setBounds(24, 20, 72, 15);
		panel1.add(label);
		
		txtContractPrice = new JTextField();
		txtContractPrice.setBounds(106, 20, 93, 21);
		panel1.add(txtContractPrice);
		txtContractPrice.setColumns(10);
		
		JLabel label_1 = new JLabel("最高价");
		label_1.setBounds(24, 55, 72, 15);
		panel1.add(label_1);
		
		txtMaxPrice = new JTextField();
		txtMaxPrice.setBounds(106, 52, 93, 21);
		panel1.add(txtMaxPrice);
		txtMaxPrice.setColumns(10);
		
		JLabel label_2 = new JLabel("最低价");
		label_2.setBounds(24, 90, 72, 15);
		panel1.add(label_2);
		
		txtMinPrice = new JTextField();
		txtMinPrice.setBounds(106, 87, 93, 21);
		panel1.add(txtMinPrice);
		txtMinPrice.setColumns(10);
		
		JLabel label_3 = new JLabel("经销方式");
		label_3.setBounds(24, 125, 79, 15);
		panel1.add(label_3);
		
		 comboBox = new JComboBox();
		comboBox.setBounds(106, 122, 100, 21);
		panel1.add(comboBox);
		
		JLabel label_4 = new JLabel("停购");
		label_4.setBounds(256, 125, 54, 15);
		panel1.add(label_4);
		
		 comboBox_1 = new JComboBox();
		comboBox_1.setBounds(339, 122, 74, 21);
		panel1.add(comboBox_1);
		
		JLabel label_5 = new JLabel("有效期");
		label_5.setBounds(24, 160, 54, 15);
		panel1.add(label_5);
		
		txtVDate = new JTextField();
		txtVDate.setBounds(106, 157, 205, 21);
		panel1.add(txtVDate);
		txtVDate.setColumns(10);
		
		btnNewButton=new JButton();
		btnNewButton.setIcon(new ImageIcon(JContractProductEditor.class.getResource("/com/openbravo/images/date.png")));
	   btnNewButton.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		btnDateStartActionPerformed(e);
		 	}
		 });
		btnNewButton.setBounds(314, 155, 58, 23);
		panel1.add(btnNewButton);
		
		add(panel1, BorderLayout.CENTER);
		
		JLabel label_6 = new JLabel("当前进价");
		label_6.setBounds(256, 20, 54, 15);
		panel1.add(label_6);
		
		JLabel label_7 = new JLabel("当前售价");
		label_7.setBounds(256, 55, 54, 15);
		panel1.add(label_7);
		
		JLabel label_8 = new JLabel("当前会员价");
		label_8.setBounds(256, 90, 73, 15);
		panel1.add(label_8);
		
		txtPriceBuy = new JTextField();
		txtPriceBuy.setEditable(false);
		txtPriceBuy.setBounds(339, 20, 66, 21);
		panel1.add(txtPriceBuy);
		txtPriceBuy.setColumns(10);
		
		txtPriceSell = new JTextField();
		txtPriceSell.setEditable(false);
		txtPriceSell.setBounds(339, 52, 66, 21);
		panel1.add(txtPriceSell);
		txtPriceSell.setColumns(10);
		
		txtPriceCustomer = new JTextField();
		txtPriceCustomer.setEditable(false);
		txtPriceCustomer.setBounds(339, 87, 66, 21);
		panel1.add(txtPriceCustomer);
		txtPriceCustomer.setColumns(10);
	}

	@Override
	public String getTabName() {
		return "合同商品";
	}

	@Override
	public void refresh() {
		
	}

	@Override
	public ContractProduct createNew(Properties prop) {
		ContractProduct obj=new ContractProduct();
		if(this.parentNode!=null)
			obj.setCustomerId(parentNode.getParentKey().getKey());
		return obj;
	}

	@Override
	public void rebuild(ContractProduct obj) throws BasicException {
		obj.setContractPrice((Double) Formats.CURRENCY.parseValue(txtContractPrice.getText()));
		obj.setMaxPrice((Double) Formats.CURRENCY.parseValue(this.txtMaxPrice.getText()));
		obj.setMinPrice((Double) Formats.CURRENCY.parseValue(this.txtMinPrice.getText()));
		obj.setContractValidityDate((Date) Formats.TIMESTAMP.parseValue(this.txtVDate.getText()));
		obj.setState((Integer) stateModel.getSelectedKey());
		obj.setDistributionMode((Integer) distributionModel.getSelectedKey());
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		super.updateUI(editAble, state);
		if (obj.getIncludeProduct() == null) {
			txtPriceBuy.setText(null);
			txtPriceSell.setText(null);
			txtPriceCustomer.setText(null);
		}else{
			txtPriceBuy.setText(Formats.CURRENCY.formatValue(obj.getIncludeProduct().getPriceBuy()));
			txtPriceSell.setText(Formats.CURRENCY.formatValue(obj.getIncludeProduct().getPriceSell()));
			txtPriceCustomer.setText(Formats.CURRENCY.formatValue(obj.getIncludeProduct().getM_dCustomerPrice()));
		}
		txtContractPrice.setText(Formats.CURRENCY.formatValue(obj.getContractPrice()));
		txtMaxPrice.setText(Formats.CURRENCY.formatValue(obj.getMaxPrice()));
		txtMinPrice.setText(Formats.CURRENCY.formatValue(obj.getMinPrice()));
		txtVDate.setText(Formats.TIMESTAMP.formatValue(obj.getContractValidityDate()));
		stateModel.setSelectedKey(obj.getState());
		distributionModel.setSelectedKey(obj.getDistributionMode());
		
		txtContractPrice.setEditable(editAble);
		txtMaxPrice.setEditable(editAble);
		txtMinPrice.setEditable(editAble);
		txtVDate.setEditable(editAble);
		comboBox.setEditable(editAble);
		comboBox_1.setEditable(editAble);
		btnNewButton.setEnabled(editAble);
	}
	
	 private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateStartActionPerformed
	        Date date;
	        try {
	            date = (Date) Formats.TIMESTAMP.parseValue(txtVDate.getText());
	        } catch (BasicException e) {
	            date = null;
	        }
	        date = JCalendarDialog.showCalendarTimeHours(this, date);
	        if (date != null) {
	        	txtVDate.setText(Formats.TIMESTAMP.formatValue(date));
	        }
	}//GEN-LAST:event_btnDateStartActionPerformed

	@Override
	public Class getEditClass() {
		return ContractProduct.class;
	}
}
